<%@page import="com.nxu.entity.CartItemBean"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.text.DecimalFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>书香阁 - 订单确认</title>
    <style>
        :root {
            --primary-color: #8d6e63;
            --light-gray: #f5f5f5;
            --dark-gray: #5a3921;
            --text-color: #333;
            --border-color: #d7ccc8;
            --success-color: #388e3c;
            --error-color: #d32f2f;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), 
                        url('https://images.unsplash.com/photo-1507842217343-583bb7270b66?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') no-repeat center center fixed;
            background-size: cover;
            color: var(--text-color);
        }
        
        .container {
            max-width: 800px;
            margin: 30px auto;
            background: rgba(255, 255, 255, 0.95);
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.2);
            backdrop-filter: blur(5px);
            animation: fadeIn 0.5s ease-in-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        h1 {
            text-align: center;
            color: var(--dark-gray);
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid var(--primary-color);
        }
        
        .order-summary {
            margin-bottom: 30px;
        }
        
        .order-summary h3 {
            color: var(--dark-gray);
            margin-bottom: 20px;
            font-size: 20px;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border-radius: 8px;
            overflow: hidden;
        }
        
        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }
        
        th {
            background-color: var(--light-gray);
            font-weight: 600;
            color: var(--dark-gray);
        }
        
        tr:hover {
            background-color: rgba(141, 110, 99, 0.05);
        }
        
        .book-title {
            font-weight: 500;
            color: var(--dark-gray);
        }
        
        .total-row {
            font-weight: bold;
            background-color: var(--light-gray);
        }
        
        .total-label {
            color: var(--dark-gray);
        }
        
        .total-amount {
            color: var(--primary-color);
            font-size: 18px;
        }
        
        .form-group {
            margin-bottom: 25px;
        }
        
        label {
            display: block;
            margin-bottom: 10px;
            font-weight: 500;
            color: var(--dark-gray);
        }
        
        input[type="text"] {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
            transition: all 0.3s;
        }
        
        input[type="text"]:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(141, 110, 99, 0.2);
            outline: none;
        }
        
        .error {
            color: var(--error-color);
            margin-top: 8px;
            font-size: 14px;
            padding: 8px;
            background-color: #ffebee;
            border-radius: 4px;
            border-left: 4px solid var(--error-color);
        }
        
        .buttons {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
        }
        
        .btn {
            padding: 12px 25px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        
        .btn-cancel {
            background-color: #f44336;
            color: white;
            border: none;
        }
        
        .btn-cancel:hover {
            background-color: #d32f2f;
            transform: translateY(-2px);
        }
        
        .btn-submit {
            background-color: var(--success-color);
            color: white;
            border: none;
        }
        
        .btn-submit:hover {
            background-color: #2e7d32;
            transform: translateY(-2px);
        }
        
        .book-cover {
            width: 40px;
            height: 60px;
            object-fit: cover;
            border-radius: 3px;
            margin-right: 15px;
            vertical-align: middle;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 25px;
            }
            
            th, td {
                padding: 10px;
            }
            
            .buttons {
                flex-direction: column;
                gap: 15px;
            }
            
            .btn {
                width: 100%;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
    <div class="container">
        <h1><i class="fas fa-clipboard-check"></i> 订单确认</h1>
        
        <div class="order-summary">
            <h3><i class="fas fa-book-open"></i> 商品清单</h3>
            <table>
                <thead>
                    <tr>
                        <th>商品名称</th>
                        <th>数量</th>
                        <th>单价</th>
                        <th>小计</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                        Map<String, CartItemBean> cart = (Map<String, CartItemBean>) session.getAttribute("cart");
                        double total = 0;
                        if (cart != null && !cart.isEmpty()) {
                            Iterator<String> iterator = cart.keySet().iterator();
                            while (iterator.hasNext()) {
                                String isbn = iterator.next();
                                CartItemBean cartItem = cart.get(isbn);
                                double subtotal = cartItem.getBook().getPrice() * cartItem.getQuantity();
                                total += subtotal;
                    %>
                    <tr>
                        <td class="book-title">
                            <img src="images/<%=cartItem.getBook().getImagefile()%>" class="book-cover" 
                                 onerror="this.src='images/default-book.jpg'" alt="<%=cartItem.getBook().getTitle()%>">
                            <%=cartItem.getBook().getTitle()%>
                        </td>
                        <td><%=cartItem.getQuantity()%></td>
                        <td>￥<%=new DecimalFormat("0.00").format(cartItem.getBook().getPrice())%></td>
                        <td>￥<%=new DecimalFormat("0.00").format(subtotal)%></td>
                    </tr>
                    <% 
                            }
                        }
                        session.setAttribute("totalAmount", total);
                    %>
                    <tr class="total-row">
                        <td colspan="3" class="total-label">总计：</td>
                        <td class="total-amount">￥<%=new DecimalFormat("0.00").format(total)%></td>
                    </tr>
                </tbody>
            </table>
        </div>
        
        <form action="<%=request.getContextPath()%>/OrderServlet" method="post">
            <input type="hidden" name="action" value="create">
            
            <div class="form-group">
                <label for="consignee"><i class="fas fa-user"></i> 收货人姓名：</label>
                <input type="text" id="consignee" name="consignee" required placeholder="请输入收货人姓名">
            </div>
            
            <div class="form-group">
                <label for="phone"><i class="fas fa-phone"></i> 联系电话：</label>
                <input type="text" id="phone" name="phone" pattern="[0-9]{11}" title="请输入11位手机号码" required placeholder="请输入11位手机号码">
            </div>
            
            <div class="form-group">
                <label for="address"><i class="fas fa-map-marker-alt"></i> 收货地址：</label>
                <input type="text" id="address" name="address" required placeholder="请输入详细收货地址">
            </div>
            
            <div class="form-group">
                <label for="postcode"><i class="fas fa-envelope"></i> 邮政编码：</label>
                <input type="text" id="postcode" name="postcode" pattern="[0-9]{6}" title="请输入6位邮政编码" required placeholder="请输入6位邮政编码">
            </div>
            
            <div class="form-group">
                <label for="cardNumber"><i class="fas fa-credit-card"></i> 支付卡号：</label>
                <input type="text" id="cardNumber" name="cardNumber" pattern="[0-9]{16,19}" title="请输入16-19位卡号" required placeholder="请输入16-19位银行卡号">
                <% if (request.getAttribute("cardError") != null) { %>
                    <div class="error">
                        <i class="fas fa-exclamation-circle"></i> <%=request.getAttribute("cardError")%>
                    </div>
                <% } %>
            </div>
            
            <div class="buttons">
                <a href="<%=request.getContextPath()%>/viewCart.jsp" class="btn btn-cancel">
                    <i class="fas fa-times"></i> 取消订单
                </a>
                <button type="submit" class="btn btn-submit">
                    <i class="fas fa-check"></i> 提交订单
                </button>
            </div>
        </form>
    </div>
</body>
</html>